package com.luhuiguo.cms.server.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.entity.Attachment;
import com.luhuiguo.cms.server.service.AttachmentService;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.shared.model.AttachmentModel;
import com.luhuiguo.cms.shared.rpc.CreateAttachment;
import com.luhuiguo.cms.shared.rpc.CreateAttachmentResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class CreateAttachmentHandler implements ActionHandler<CreateAttachment, CreateAttachmentResult> {

    @Autowired
    private AttachmentService attachmentService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public CreateAttachmentResult execute(CreateAttachment action, ExecutionContext context) throws ActionException {

        Attachment a = MapperUtils.map(action.get(), Attachment.class);

        attachmentService.saveAttachment(a);
        return new CreateAttachmentResult(MapperUtils.map(a, AttachmentModel.class));

    }

    @Override
    public Class<CreateAttachment> getActionType() {
        return CreateAttachment.class;
    }

    @Override
    public void rollback(CreateAttachment action, CreateAttachmentResult result, ExecutionContext context)
            throws ActionException {

    }

}
